package com.plugin.manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.plugin.core.model.entity.ElderHealthCheckException;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 长者健康检查异常记录Mapper接口
 * 
 * @author mark
 * @date 2025-07-26
 */
@Mapper
public interface ElderHealthCheckExceptionMapper extends BaseMapper<ElderHealthCheckException> {

    /**
     * 根据条件查询健康检查异常记录
     *
     * @param elderName 长者姓名
     * @param bedNumber 床位号
     * @param exceptionDesc 异常描述（模糊查询）
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 健康检查异常记录列表
     */
    @Select("<script>" +
            "SELECT * FROM elder_health_check_exception " +
            "WHERE del = 0 " +
            "<if test='elderName != null'>" +
            " AND elder_name LIKE CONCAT('%', #{elderName}, '%') " +
            "</if>" +
            "<if test='bedNumber != null'>" +
            " AND bed_number = #{bedNumber} " +
            "</if>" +
            "<if test='exceptionDesc != null'>" +
            " AND (exception_desc LIKE CONCAT('%', #{exceptionDesc}, '%') " +
            " OR exception_type LIKE CONCAT('%', #{exceptionDesc}, '%')) " +
            "</if>" +
            "<if test='startTime != null'>" +
            " AND check_time &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            " AND check_time &lt; #{endTime} " +
            "</if>" +
            " ORDER BY create_time DESC, id DESC limit 5" +
            "</script>")
    List<ElderHealthCheckException> getHealthCheckExceptionsByConditions(
            @Param("elderName") String elderName,
            @Param("bedNumber") String bedNumber,
            @Param("exceptionDesc") String exceptionDesc,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );

    /**
     * 根据长者ID查询健康检查异常记录
     * 
     * @param elderId 长者ID
     * @return 健康检查异常记录列表
     */
    @Select("SELECT * FROM elder_health_check_exception " +
            "WHERE elder_id = #{elderId} AND del = 0 " +
            "ORDER BY check_time DESC, id DESC")
    List<ElderHealthCheckException> getHealthCheckExceptionsByElderId(@Param("elderId") Long elderId);

    /**
     * 获取最近的健康检查异常记录
     * 
     * @param limit 限制条数
     * @return 健康检查异常记录列表
     */
    @Select("SELECT * FROM elder_health_check_exception " +
            "WHERE del = 0 " +
            "ORDER BY check_time DESC, id DESC " +
            "LIMIT #{limit}")
    List<ElderHealthCheckException> getRecentHealthCheckExceptions(@Param("limit") Integer limit);

    /**
     * 根据处理状态查询健康检查异常记录
     * 
     * @param handleStatus 处理状态
     * @return 健康检查异常记录列表
     */
    @Select("SELECT * FROM elder_health_check_exception " +
            "WHERE handle_status = #{handleStatus} AND del = 0 " +
            "ORDER BY check_time DESC, id DESC")
    List<ElderHealthCheckException> getHealthCheckExceptionsByHandleStatus(@Param("handleStatus") Integer handleStatus);

    /**
     * 统计健康检查异常记录数量
     *
     * @param elderName 长者姓名
     * @param bedNumber 床位号
     * @param exceptionDesc 异常描述
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 记录数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM elder_health_check_exception " +
            "WHERE del = 0 " +
            "<if test='elderName != null'>" +
            " AND elder_name LIKE CONCAT('%', #{elderName}, '%') " +
            "</if>" +
            "<if test='bedNumber != null'>" +
            " AND bed_number = #{bedNumber} " +
            "</if>" +
            "<if test='exceptionDesc != null'>" +
            " AND (exception_desc LIKE CONCAT('%', #{exceptionDesc}, '%') " +
            " OR exception_type LIKE CONCAT('%', #{exceptionDesc}, '%')) " +
            "</if>" +
            "<if test='startTime != null'>" +
            " AND check_time &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            " AND check_time &lt;= #{endTime} " +
            "</if>" +
            "</script>")
    Long countHealthCheckExceptionsByConditions(
            @Param("elderName") String elderName,
            @Param("bedNumber") String bedNumber,
            @Param("exceptionDesc") String exceptionDesc,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );
}
